<?php
class answermod extends MY_Model{
	
	function answermod(){
		parent::__construct(
			"answer",
			array(
				"id_answer",
				"author",
				"subject",
				"radio",
				"session",
				"answer",
				"opt",			
				"posted"
			),
			array("id_answer")
		);
	}
	
	function generateSession($data){
		return do_hash($data.(unix_to_human(time(),true,'eu')."answerNewSession").$this->table_name);
	}
	
	function add($newData){
		$newData['posted'] = unix_to_human(time(),true,'eu');
		$this->db->insert($this->table_name, $newData);
	}
	
	function update($id,$newData){
		$this->db->where($this->table_fields[0], $id);
		return $this->db->update($this->table_name, $newData);
	}
	
	function delete($ids){
		if (!is_array($ids)) $ids = array($ids);
    	foreach($ids as $pk) {
    		$this->db->or_where($this->table_fields[0], $pk);	
    	}
    	return $this->db->delete($this->table_name, $pk);
	}
	
	function remove($ids){
    	return $this->db->delete($this->table_name, array($this->table_fields[0]=>$ids));
    }
	
 	function get($id){
		return $this->db->get_where($this->table_name, array($this->table_fields[0]=>$id));
    }
    
	function get_record($limit=0,$offset=20){
		$this->db->limit($offset,$limit);
		$this->db->order_by('posted','desc');
		return $this->db->get($this->table_name);
    }
    
	function get_record_unlimit(){
		$this->db->order_by('posted','desc');
		return $this->db->get($this->table_name);
    }

	function get_where($where,$limit=0,$offset=100){
//    	return $this->db->get_where($this->table_name, $where, $limit, $offset);
		$this->db->like('id_answer',$where,'match');
		$this->db->or_like('author',$where,'match');
		$this->db->or_like('subject',$where,'both');
		$this->db->or_like('radio',$where,'both');
		$this->db->or_like('session',$where,'both');
		$this->db->or_like('answer',$where,'both');
		$this->db->or_like('opt',$where,'both');
		$this->db->or_like('posted',$where,'both');
		$this->db->order_by('posted','desc');
		$this->db->limit($offset,$limit);
    	return $this->db->get($this->table_name);
    } 
    
    function get_where_subject_true($id,$session,$limit=0,$offset=100){
    	return $this->db->get_where($this->table_name, array('subject'=>$id, 'session'=>$session, 'opt'=>'true'), $limit, $offset);
//		$this->db->like('id_lesson',$id,'match');
//		$this->db->order_by('posted','desc');
//		$this->db->limit($offset,$limit);
//    	return $this->db->get($this->table_name);
    } 
    
    function get_where_subject_false($id,$session,$limit=0,$offset=100){
    	return $this->db->get_where($this->table_name, array('subject'=>$id, 'session'=>$session, 'opt'=>'false'), $limit, $offset);
//		$this->db->like('id_lesson',$id,'match');
//		$this->db->order_by('posted','desc');
//		$this->db->limit($offset,$limit);
//    	return $this->db->get($this->table_name);
    } 
    
	
}